/*******************************************************************************
*LAST EDITED: 01/30/2022
*CONTRIBUTORS: BC, MEK & AO
*PROJECT: Is It Who You Are or What You Get? Comparing the Impacts of Loans 
		  and Grants for Microenterprise Development
*JOURNAL: American Economic Journal-Applied Economics
*DESCRIPTION: This program builds all in-text Tables
*******************************************************************************/
***Set up 
	clear all
	set rmsg on
	set seed 12345
	set maxvar 32767  // Modify if not StataMP 
	set max_memory . // (.) operating system is willing to supply all memory
	set more off
	set matsize 10000
	set scheme s2color
	grstyle init
	grstyle set plain, horizontal grid
***Directories
	*Root 
		*global root "c\tmp\" // Include and update this line if you're running this do file individually and not through "00_SetUp.do"
	*Analysis data
		global adata "$root\Data\"
	*Save tables
		global tabs "$root\Output\Tables"
********************************************************************************

			***********************************************************
			***********************************************************
						      ***Tables in text***
			***********************************************************
			***********************************************************
/*
INPUT: This program requires 3 datasets 
	1) baseline_rand_noPII_edited.dta
	2) endline_rand_noPII_edited.dta
	3) baseline_endline_rand_noPII_edited.dta
	4) attrition_baseline_noPII_edited.dta
OUTPUT: The program generates Table 1 to Table 10 in paper 
*/			

****************************
***Table 1. Baseline Balance
****************************
	*Call data
		use "$adata\AEJ_CKO_baseline_edited.dta",clear 
		ren b_* *
	*Loop to summarize variables of interest
		foreach var of varlist agebase baseline_gender educ_college educ_hs ///
		educ_lths newworked_before marital_* family_inc_low has_biz any_borrowing ///
		std_broad_pressure has_kids {
			areg `var' i.treatment, a(round) ro
			est store balance_`var'
			*Control mean
			summarize `var'  if   treatment==0
			estadd scalar Mean = r(mean)
			estadd scalar Sd = r(sd)
			estout , stats(Mean Sd) 
			eststo 
		}
	*Recieved training 
		areg received_train i.treatment if treatment!=0, a(round) ro
		est store balance_received_train 
	*Output 
		esttab balance_* using "$tabs\Main_Tables\Tab1_BaselineBalance.csv", ///
		cells(b(star fmt(%9.6f)) se(fmt(%9.6f))) order(_cons treatment) ///
		stardetach stats(Mean Sd N, fmt(%9.3g)) style(tab) varwidth(8) ///
		modelwidth(8) plain replace

	*Global p-val/Multinomial logit
		mlogit treatment agebase baseline_gender educ_college educ_hs ///
		educ_lths newworked_before marital_* family_inc_low has_biz ///
		any_borrowing std_broad_pressure has_kids i.round , vce(robust)
		di "p-value for model test: `e(p)'"
		
		test _b[in_kind_grant:agebase]=_b[in_kind_grant:baseline_gender]=_b[in_kind_grant:educ_college]=_b[in_kind_grant:educ_hs]=_b[in_kind_grant:educ_lths]=_b[in_kind_grant:newworked_before]=_b[in_kind_grant:marital_m]=_b[in_kind_grant:family_inc_low]=_b[in_kind_grant:has_biz]=_b[in_kind_grant:any_borrowing]=_b[in_kind_grant:std_broad_pressure]=_b[in_kind_grant:has_kids]=_b[cash_grant:agebase]=_b[cash_grant:baseline_gender]=_b[cash_grant:educ_college]=_b[cash_grant:educ_hs]=_b[cash_grant:educ_lths]=_b[cash_grant:newworked_before]=_b[cash_grant:marital_m]=_b[cash_grant:family_inc_low]=_b[cash_grant:has_biz]=_b[cash_grant:any_borrowing]=_b[cash_grant:std_broad_pressure]=_b[cash_grant:has_kids]=_b[loan:agebase]=_b[loan:baseline_gender]=_b[loan:educ_college]=_b[loan:educ_hs]=_b[loan:educ_lths]=_b[loan:newworked_before]=_b[loan:marital_m]=_b[loan:family_inc_low]=_b[loan:has_biz]=_b[loan:any_borrowing]=_b[loan:std_broad_pressure]=_b[loan:has_kids]==0
		
	*Stats for control group			
		foreach var of varlist agebase baseline_gender educ_college educ_hs ///
	educ_lths newworked_before marital_* family_inc_low has_biz any_borrowing ///
	std_broad_pressure has_kids {
		summarize `var'  if  treatment==0
	}	
	
	
	*Number of observations for each treatment group in balance tables
	tab treatment if agebase!=.
	
	*Response Rate
		ren * b_*
		ren b_u_key u_key
		merge 1:1 u_key using "$adata\AEJ_CKO_endline_edited.dta", force gen(combo)
		gen attrition=combo==1
		gen response=1-attrition
		areg response i.b_treatment, a(b_round) ro
			
			
***************************************************************
***Table 2. Compliance with the experimental protocol by gender
***************************************************************		
*Call and sort data
	use "$adata\AEJ_CKO_baseline_edited.dta",clear 
	ren b_* *
	*Vars of interest 
		global outcomeC amount_received_corrected loanR  In_KindR  CashR amount_received_condition
		*For Men
			eststo clear
			foreach var in $outcomeC {
			 reg `var' Loan In_Kind Cash if male==1,  ro
			eststo 
			} 
			esttab  using "$tabs/Main_Tables/Tab2_Compliance(Male)_rerun.tex",  replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3 )   frag nonumbers obslast  nomtitle ///"
			keep(Loan In_Kind Cash _cons) nonotes width(\hsize) line nogaps
		*For Women
			eststo clear
			foreach var in $outcomeC {
			 reg `var' Loan In_Kind Cash if female==1,  ro
			eststo 
			} 
			esttab  using "$tabs/Main_Tables/Tab2_Compliance(Female)_rerun.tex",  replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)   frag nonumbers obslast  nomtitle ///"
			keep(Loan In_Kind Cash _cons) nonotes width(\hsize) line nogaps

************************************************
***Table 3. Utilization of Financial Instruments
************************************************
	*Call data
		use "$adata\AEJ_CKO_endline_edited.dta",clear 
	*Vars of interest 
		gen total_savings=rosca_savings+saved_amount_w
		global outcomeL "any_loan total_outside_loans total_external_funding total_savings" 
	*For Men
		eststo clear
		foreach var in $outcomeL {
			areg `var' Loan In_Kind Cash if male==1, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==1, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
		*Output
			esttab using "$tabs/Main_Tables/Tab3_Loan(Male).tex",  replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)  ///
			stats(Mean Joint Same  N, fmt(3 3 3 0)) ///
			sfmt(%9.2f %9.2f %9.2f %9.0f) frag nonumbers obslast  nomtitle ///
			keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps 		
			
	*For Women
		eststo clear
		foreach var in $outcomeL {
			areg `var' Loan In_Kind Cash if male==0, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==0, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
		*Output
			esttab using "$tabs/Main_Tables/Tab3_Loan(Female).tex",  replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)  ///
			stats(Mean Joint Same  N, fmt(3 3 3 0)) ///
			sfmt(%9.2f %9.2f %9.2f %9.0f) frag nonumbers obslast nomtitle ///
			keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps 
 
	***P-value for joint test of whether the female coefficients are equal to the male coefficients.
		xi i.treatment*i.female, pre(_X) noomit

		foreach var in $outcomeL {
			areg `var' _XtreXfem_1_1 _XtreXfem_2_1 _XtreXfem_3_1 _XtreXfem_1_0  _XtreXfem_2_0  _XtreXfem_3_0  _Xfemale_1 , a(b_round) ro
			test (_b[_XtreXfem_1_1]-_b[_XtreXfem_1_0]=0)  (_b[_XtreXfem_2_1]-_b[_XtreXfem_2_0]=0) (_b[_XtreXfem_3_1]-_b[_XtreXfem_3_0]=0)
		}

*****************************************
***Table 4. Impacts on Business Outcomes
*****************************************
*Call data
	use "$adata\AEJ_CKO_endline_edited.dta",clear 
	*Vars of interest 
		global outcome "e_hasbiz new_biz_assets_w monthly_revenue_w monthly_expenses_w monthly_profits_w std_biz_index"
		*For Men 
			eststo clear
			foreach var in $outcome {
				areg `var' Loan In_Kind Cash if male==1, a(b_round) ro
				test Loan In_Kind Cash
				estadd scalar Joint = r(p)
				test Loan=In_Kind=Cash
				estadd scalar Same = r(p)
				summarize `var'  if   treatment==0 & male==1, meanonly
				estadd scalar Mean = r(mean)
				estout, stats(Mean Joint Same)
				eststo 
			} 
			*Output
				esttab using "$tabs/Main_Tables/Tab4_business(Male).tex",  replace ///
				label tex star(* 0.10 ** 0.05 *** 0.01) b(2) se(3 )  ///
				stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
				frag nonumbers obslast  nomtitle ///
				keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps
				eststo clear
		*For Women 
			eststo clear
			foreach var in $outcome {
				areg `var' Loan In_Kind Cash if female==1, a(b_round) ro
				test Loan In_Kind Cash
				estadd scalar Joint = r(p)
				test Loan=In_Kind=Cash
				estadd scalar Same = r(p)
				summarize `var'  if   treatment==0 & female==1, meanonly
				estadd scalar Mean = r(mean)
				estout, stats(Mean Joint Same )
				eststo 
			} 
			*Output
				esttab using "$tabs/Main_Tables/Tab4_business(Female).tex",  replace ///
				label tex star(* 0.10 ** 0.05 *** 0.01) b(2) se(3)  ///
				stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
				frag nonumbers obslast  nomtitle ///
				keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps

	***P-value for joint test of whether the female coefficients are equal to the male coefficients.
		xi i.treatment*i.female, pre(_X) noomit

		foreach var in $outcome {
			areg `var' _XtreXfem_1_1 _XtreXfem_2_1 _XtreXfem_3_1 _XtreXfem_1_0  _XtreXfem_2_0  _XtreXfem_3_0  _Xfemale_1 , a(b_round) ro
			test (_b[_XtreXfem_1_1]-_b[_XtreXfem_1_0]=0)  (_b[_XtreXfem_2_1]-_b[_XtreXfem_2_0]=0) (_b[_XtreXfem_3_1]-_b[_XtreXfem_3_0]=0)
		}
	
				
********************************************
***Table 5. Impacts on Employment and Income
********************************************
*Call and sort data
	use "$adata\AEJ_CKO_endline_edited.dta",clear 
	sort treatment
	*Vars of interest 
		global outcomeE "e_works_at_all monthly_profits_w wage_inc_w labor_income e_total_income_w"
	*For Men
		eststo clear
		foreach var in $outcomeE {
			areg `var' Loan In_Kind Cash if male==1, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==1, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
		*Output
			esttab using "$tabs/Main_Tables/Tab5_Income(Male).tex", replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)  ///
			stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
			frag nonumbers obslast  nomtitle ///
			keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps
	*For Women 
		eststo clear
		foreach var in $outcomeE {
			areg `var' Loan In_Kind Cash if male==0, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==0, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
		*Output
			esttab using "$tabs/Main_Tables/Tab5_Income(Female).tex", replace ///
			label tex star(* 0.10 ** 0.05 *** 0.01) b(3 ) se(3) ///
			stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
			frag nonumbers obslast  nomtitle ///
			keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps

	***P-value for joint test of whether the female coefficients are equal to the male coefficients.
		xi i.treatment*i.female, pre(_X) noomit
		
		foreach var in $outcomeE {
			areg `var' _XtreXfem_1_1 _XtreXfem_2_1 _XtreXfem_3_1 _XtreXfem_1_0  _XtreXfem_2_0  _XtreXfem_3_0  _Xfemale_1 , a(b_round) ro
			test (_b[_XtreXfem_1_1]-_b[_XtreXfem_1_0]=0)  (_b[_XtreXfem_2_1]-_b[_XtreXfem_2_0]=0) (_b[_XtreXfem_3_1]-_b[_XtreXfem_3_0]=0)
		}	

				
			
********************
***Table 6. Time Use
********************
*Call and sort data
	use "$adata\AEJ_CKO_endline_edited.dta",clear 
	*Vars of interest 
		gen child_chores=childcare_home_time_w+chores_home_time_w 
		global time_use "e_weekly_hours_wage_w e_weekly_hours_self_w agri_home_time_w child_chores std_time_index"
	*For Men
		eststo clear
		foreach var in $time_use {
			areg `var' Loan In_Kind Cash if male==1, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==1, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
			*Output
				esttab using "$tabs/Main_Tables/Tab6_time(Male).tex", replace ///
				label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3 )  ///
				stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
				frag nonumbers obslast  nomtitle ///
				keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps
	*For Women 
		eststo clear
		foreach var in $time_use {
			areg `var' Loan In_Kind Cash if male==0, a(b_round) ro
			test Loan In_Kind Cash
			estadd scalar Joint = r(p)
			test Loan=In_Kind=Cash
			estadd scalar Same = r(p)
			summarize `var'  if   treatment==0 & male==0, meanonly
			estadd scalar Mean = r(mean)
			estout , stats(Mean Joint Same )
			eststo 
		} 
			*Output
				esttab  using "$tabs/Main_Tables/Tab6_time(Female).tex", replace ///
				label tex star(* 0.10 ** 0.05 *** 0.01) b(3) se(3 )  ///
				stats(Mean Joint Same  N) sfmt(%9.2f %9.2f %9.2f %9.0f) ///
				frag nonumbers obslast  nomtitle ///
				keep(Loan In_Kind Cash) nonotes width(\hsize) line nogaps

	***P-value for joint test of whether the female coefficients are equal to the male coefficients.
		xi i.treatment*i.female, pre(_X) noomit

		foreach var in $time_use {
			areg `var' _XtreXfem_1_1 _XtreXfem_2_1 _XtreXfem_3_1 _XtreXfem_1_0  _XtreXfem_2_0  _XtreXfem_3_0  _Xfemale_1 , a(b_round) ro
			test (_b[_XtreXfem_1_1]-_b[_XtreXfem_1_0]=0)  (_b[_XtreXfem_2_1]-_b[_XtreXfem_2_0]=0) (_b[_XtreXfem_3_1]-_b[_XtreXfem_3_0]=0)
		}

		
****************************************************************
***Table 7. Testing Heterogeneity Within & Across Treatment Arms
****************************************************************

****Note that due to some random failures in the bootstrapping procedure we are unable to exactly replicate the p-values from this table. The values are very similar and do not affect the interpretation of the results. 

*Call data
	use "$adata\AEJ_CKO_endline_edited.dta",clear 
	*log using "$tabs/Main_Tables/Tab7_Quantile_Tests.smcl", replace
	log using "$tabs/Main_Tables/Tab7_Quantile_Tests_rerun.smcl", replace
	*Set Seed 
		set seed 607
	*Quantile Regression 
		sqreg labor_income female LoanF In_KindF CashF LoanM In_KindM CashM, ///
		quantile(.250 .375 .500 .625 .750 .875 .900 .950 .975) reps(10000)

	*Female 
		*Panel A:Row 1: Quantile effects in each treatment arm are equal
			test ([q250]LoanF=[q500]LoanF=[q625]LoanF=[q750]LoanF=[q875]LoanF=[q900]LoanF=[q950]LoanF=[q975]LoanF=[q375]LoanF) ///
			([q250]In_KindF=[q500]In_KindF=[q625]In_KindF=[q750]In_KindF=[q875]In_KindF=[q900]In_KindF=[q950]In_KindF=[q975]In_KindF=[q375]In_KindF) ///
			([q250]CashF=[q500]CashF=[q625]CashF=[q750]CashF=[q875]CashF=[q900]CashF=[q950]CashF=[q975]CashF=[q375]CashF)  
		*Panel A:Row 2: Quantile effects across treatment arms are equal
			test ([q250]LoanF = [q250]In_KindF = [q250]CashF) ///
			([q500]LoanF = [q500]In_KindF = [q500]CashF) ///
			([q625]LoanF = [q625]In_KindF = [q625]CashF) ///
			([q750]LoanF = [q750]In_KindF = [q750]CashF) ///
			([q750]LoanF = [q750]In_KindF = [q750]CashF) ///
			([q875]LoanF = [q875]In_KindF = [q875]CashF) ///
			([q900]LoanF = [q900]In_KindF = [q900]CashF) ///
			([q950]LoanF = [q950]In_KindF = [q950]CashF) ///
			([q375]LoanF = [q375]In_KindF = [q375]CashF) ///
			([q975]LoanF = [q975]In_KindF = [q975]CashF) 

	*Male
		*Panel B:Row 1: Quantile effects in each treatment arm are equal
		test ([q250]LoanM=[q500]LoanM=[q625]LoanM=[q750]LoanM=[q875]LoanM=[q900]LoanM=[q950]LoanM=[q975]LoanM=[q375]LoanM) ///
			([q250]In_KindM=[q500]In_KindM=[q625]In_KindM=[q750]In_KindM=[q875]In_KindM=[q900]In_KindM=[q950]In_KindM=[q975]In_KindM=[q375]In_KindM) ///
			([q250]CashM=[q500]CashM=[q625]CashM=[q750]CashM=[q875]CashM=[q900]CashM=[q950]CashM=[q975]CashM=[q375]CashM)   
		*Panel B:Row 2: Quantile effects across treatment arms are equal
			test ([q250]LoanM = [q250]In_KindM = [q250]CashM) ///
			([q500]LoanM = [q500]In_KindM = [q500]CashM) ///
			([q625]LoanM = [q625]In_KindM = [q625]CashM) ///
			([q750]LoanM = [q750]In_KindM = [q750]CashM) ///
			([q750]LoanM = [q750]In_KindM = [q750]CashM) ///
			([q875]LoanM = [q875]In_KindM = [q875]CashM) ///
			([q900]LoanM = [q900]In_KindM = [q900]CashM) ///
			([q950]LoanM = [q950]In_KindM = [q950]CashM) ///
			([q975]LoanM = [q975]In_KindM = [q975]CashM) ///
			([q375]LoanM = [q375]In_KindM = [q375]CashM) 
		log close 	

	log using "$tabs/Main_Tables/log_ranksum_permutation.smcl",replace

	*Men*
		capture  prog drop test_3_male
		prog test_3_male, rclass
		ranksum e_total_income_w if male & (treatment==1 | treatment==2), by(treatment)
		sca S_L_I_m=abs(r(z))
		ranksum e_total_income_w if male & (treatment==1 | treatment==3), by(treatment)
		sca S_I_C_m=abs(r(z))
		ranksum e_total_income_w if male & (treatment==2 | treatment==3), by(treatment)
		sca S_C_L_m=abs(r(z))
		sca Stat_m=S_L_I_m+S_I_C_m+S_C_L_m
		return scalar Stat = S_L_I_m+S_I_C_m+S_C_L_m
		end 
		
		permute treatment Stat=r(Stat) if male==1, strata(b_round) reps(1000) : test_3_male

	*Women*
		capture  prog drop test_3_female
		prog test_3_female, rclass
		ranksum e_total_income_w if female & (treatment==1 | treatment==2), by(treatment)
		sca S_L_I_f=abs(r(z))
		ranksum e_total_income_w if female & (treatment==1 | treatment==3), by(treatment)
		sca S_I_C_f=abs(r(z))
		ranksum e_total_income_w if female & (treatment==2 | treatment==3), by(treatment)
		sca S_C_L_f=abs(r(z))
		sca Stat_f=S_L_I_f+S_I_C_f+S_C_L_f
		return scalar Stat = S_L_I_f+S_I_C_f+S_C_L_f
		end 
		
		permute treatment Stat=r(Stat) if female, strata(b_round) reps(10000) : test_3_female

		log close			

************************************************************************
***Table 8. Balancing among top 25 participants in each assignment group
************************************************************************

****Note that due to some random failures in the bootstrapping procedure we are unable to exactly replicate the p-values from this table. The values are very similar and do not affect the interpretation of the results. 

	*Call data
		use "$adata\AEJ_CKO_baseline_endline_edited.dta",clear 
	*Vars of interest 
		*global X "b_agebase b_educ_college b_educ_hs b_educ_lths b_newworked_before b_marital_s b_marital_m b_family_inc_low b_has_biz b_any_borrowing std_broad_pressure b_has_kids"
		
		global X "b_agebase b_educ_college b_educ_hs b_educ_lths b_newworked_before b_marital_s b_marital_m b_family_inc_low b_has_biz b_any_borrowing std_broad_pressure b_has_kids"
		
	**Panel A: Top 25% Female 
		*Balance Columns 1-4
			fsum $X if treatment==0 & top25_female==1
			fsum $X if treatment==1 & top25_female==1
			fsum $X if treatment==2 & top25_female==1
			fsum $X if treatment==3 & top25_female==1
		*Column 5 
			foreach var in $X{
				areg `var' In_Kind Cash if treatment!=0 & top25_female, a(b_round) ro
				test In_Kind=Cash=0
				estadd scalar Same = r(p)
				estout , stats(Same )
				eststo `var'
			} 
			*Export Results 
				esttab  using "$tabs/Main_Tables/Tab8_BalancingTop25_col5(Female).csv",  replace ///
				cells(b se(fmt(%9.6f))) order(_cons treatment) ///
				stats(N Same, fmt(%9.3g)) style(tab) varwidth(8) ///
				modelwidth(8) plain 
		*Column 6 
			eststo clear
				foreach var in $X{
					areg `var' Loan In_Kind Cash if top25_female, a(b_round) ro
					test Loan=In_Kind=Cash=0
					estadd scalar Same = r(p)
					estout , stats(Same )
					eststo `var'
				} 
			*Export Results 	
				esttab  using "$tabs/Main_Tables/Tab8_BalancingTop25_col6(Female).csv",  replace ///
				cells(b se(fmt(%9.6f))) order(_cons treatment) ///
				stats(N Same, fmt(%9.3g)) style(tab) varwidth(8) modelwidth(8) plain 

	**Panel B: Top 25% Male
		*Balance Columns 1-4
			fsum $X if treatment==0 & top25_male==1
			fsum $X if treatment==1 & top25_male==1
			fsum $X if treatment==2 & top25_male==1
			fsum $X if treatment==3 & top25_male==1
		*Column 5  
			eststo clear
			foreach var in $X{
				areg `var' In_Kind Cash if treatment!=0 & top25_male, a(b_round) ro
				test In_Kind=Cash
				estadd scalar Same = r(p)
				estout , stats(Same)
				eststo `var'
			} 
			*Export Results 	
				esttab using "$tabs/Main_Tables/Tab8_BalancingTop25_col5(Male).csv", replace ///
				cells(b se(fmt(%9.6f))) order(_cons treatment) ///
				stats(N Same, fmt(%9.3g)) style(tab) varwidth(8) modelwidth(8) plain 
		*Column 6 		
			eststo clear
			foreach var in $X{
				areg `var' Loan In_Kind Cash if top25_male, a(b_round) ro
				test Loan=In_Kind=Cash
				estadd scalar Same = r(p)
				estout , stats(Same)
				eststo `var'
			} 
			*Export Results
				esttab using "$tabs/Main_Tables/Tab8_BalancingTop25_col6(Male).csv",  replace ///
				cells(b se(fmt(%9.6f))) order(_cons treatment) ///
				stats(N Same, fmt(%9.3g)) style(tab) varwidth(8) modelwidth(8) plain 

	**Joint Test p-value for Panel A and B 
		*Gen needed 
			capture drop treatment_A
			gen treatment_A=treatment
			replace treatment_A=. if treatment_A==0
		* Panel A: Female 
			*3 Treatment Groups / Column 5
				estimates clear 
				*Log to store results 
					log using "$tabs/Main_Tables/Tab8_JointPVAL_3T(Female).smcl",replace
				*Define program
					capture prog drop balance_A
						program balance_A , rclass
							mlogit treatment_A $X i.b_round if top25_female==1 & female==1 & treatment!=0, base(1)  diff iterate(100)
							capture drop p2
							predict p2 if e(sample), outcome(2)
							capture drop p3
							predict p3 if e(sample), outcome(3)
							capture drop p1
							gen p1=1-p2-p3
							capture drop s_i
							gen s_i_0=log(0.000001+p1)*(treatment_A==1)+log(0.000001+p2)*(treatment_A==2)+log(0.000001+p3)*(treatment_A==3)
							sum s_i,meanonly
							return scalar Stat = r(mean)
						end
					*Permutations
						permute treatment_A Stat=r(Stat) if top25_female==1 & female==1 & treatment!=0, strata(b_round) reps(10000) : balance_A
						log close
			*4 Assigment Groups / Column 6
				estimates clear 
				*Log to store results 
					log using "$tabs/Main_Tables/Tab8_JointPVAL_4G(Female).smcl",replace
				*Define program 
					capture program drop balance 
					program balance , rclass
						mlogit treatment $X i.b_round if top25_female==1 & female==1, base(0) diff iterate(100)
						capture drop p1
						predict p1 if e(sample), outcome(1)
						capture drop p2
						predict p2 if e(sample), outcome(2)
						capture drop p3
						predict p3 if e(sample), outcome(3)
						capture drop p0
						gen p0=1-p1-p2-p3
						capture drop s_i
						gen s_i_0=log(0.000001+p0)*(treatment==0)+log(0.000001+p1)*(treatment==1)+log(0.000001+p2)*(treatment==2)+log(0.000001+p3)*(treatment==3)
						sum s_i,meanonly
						return scalar Stat = r(mean)
					end
				*Permutations
					permute treatment Stat=r(Stat) if top25_female==1 & female==1, strata(b_round) reps(10000) : balance
					log close
		* Panel B: Male 
			*3 Treatment Groups / Column 5 
				estimates clear
				*Log to store results 
					log using "$tabs/Main_Tables/Tab8_JointPVAL_3T(Male).smcl",replace
				*Define program 
				capture prog drop balance_A
					program balance_A , rclass
						mlogit treatment_A $X i.b_round if top25_male==1 & male==1 & treatment!=0, base(1)  diff iterate(100)
						capture drop p2
						predict p2 if e(sample), outcome(2)
						capture drop p3
						predict p3 if e(sample), outcome(3)
						capture drop p1
						gen p1=1-p2-p3
						capture drop s_i
						gen s_i_0=log(0.000001+p1)*(treatment_A==1)+log(0.000001+p2)*(treatment_A==2)+log(0.000001+p3)*(treatment_A==3)
						sum s_i,meanonly
						return scalar Stat = r(mean)
					end
				*Permutations 
					permute treatment_A Stat=r(Stat) if top25_male==1 & male==1 & treatment!=0, strata(b_round) reps(10000) : balance_A
					log close
			*4 Assigment Groups / Column 6
				estimates clear
				*Log to store results 
					log using "$tabs/Main_Tables/Tab8_JointPVAL_4G(Male).smcl",replace
				*Define program 
					capture program drop balance 
						program balance , rclass
							mlogit treatment $X i.b_round if top25_male==1 & male==1, base(0) diff iterate(100)
							capture drop p1
							predict p1 if e(sample), outcome(1)
							capture drop p2
							predict p2 if e(sample), outcome(2)
							capture drop p3
							predict p3 if e(sample), outcome(3)
							capture drop p0
							gen p0=1-p1-p2-p3
							capture drop s_i
							gen s_i_0=log(0.000001+p0)*(treatment==0)+log(0.000001+p1)*(treatment==1)+log(0.000001+p2)*(treatment==2)+log(0.000001+p3)*(treatment==3)
							sum s_i,meanonly
							return scalar Stat = r(mean)
						end
				*Permutations 
					permute treatment Stat=r(Stat) if top25_male==1 & male==1, strata(b_round) reps(10000) : balance
					log close	
